package sort;

import org.junit.Test;

/**
 * @author DuJiabao
 * @create 2020-12-30 21:18
 */
public class SelectionSort extends Example {
    public static void sort(Comparable[] array) {
        for (int i = 0; i < array.length; i++) {
            int min = i;
            for (int j = i + 1; j < array.length; j++) {
                if (less(array[j], array[min])) {
                    min = j;
                }
            }
            swap(array, i, min);
        }
    }

    @Test
    public void test() {
        long currentTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            Integer[] arr = new Integer[]{10, -1, 2, 11, 23, -100, 40, 20, 20, 30, 40, 10, 30, 30, 22, 30, 10, 2, 3, 2, 10, 20};
            sort(arr);
        }
        Integer[] arr = new Integer[]{10, -1, 2, 11, 23, -100, 40, 20, 20, 30, 40, 10, 30, 30, 22, 30, 10, 2, 3, 2, 10, 20};
        sort(arr);
        System.out.println("isSorted: " + isSorted(arr));
        System.out.println("Use time: " + (System.currentTimeMillis() - currentTime));
        show(arr);
    }
}
